package com.xy.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.xy.common.jpa.AbstractTimestampEntity;
import com.xy.entity.Coupon.Coupon;
import com.xy.entity.msg.ActivityMsg;
import com.xy.entity.msg.SystemMsg;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Cascade;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Thinkpad on 2018/1/4.
 */

@Entity
@Data
@Table(name = "user")
@EqualsAndHashCode(exclude ={"systemMsgs","activityMsgs","coupons"})

public class User extends AbstractTimestampEntity implements UserDetails {

    //    @JSONField
    @ApiModelProperty(value="用户名",name="username")
    private String username;//账号
    @JSONField(serialize = false)
    @ApiModelProperty(value="密码",name="password")
    private String password;//密码
    @ApiModelProperty(value="性别",name="gender",hidden = true)
    private Integer gender;//性别
    private Integer age;//年龄
    @JSONField(format = "yyyy-MM-dd")
    private Date birth;//出生年月
    private String phone;//手机号
    private String email;//邮箱
    @JSONField(serialize = false)
    @ApiModelProperty(value="信用",hidden = true)
    private String credit;//信用
    private BigDecimal balance;//余额
    //    @Column(name = "pay_password",nullable = false)
    @JSONField(serialize = false)
    @ApiModelProperty(value="支付密码",hidden = true)
    private String payPassword;//支付密码
    private String nickname;//昵称
    private String head;//头像
    @JSONField(serialize = false)
    @ApiModelProperty(value="会员级别",hidden = true)
    private String member;//会员级别
    private Integer integral;//积分
//    @JSONField(serialize = false)
    @ApiModelProperty(value="邀请码",name="invitcode")
    private Integer invitcode;//邀请码
    @JSONField(serialize = false)
    @ApiModelProperty(value="用户状态",hidden = true)
    private Integer state;//用户状态

    @JSONField(serialize = false)
    private Date lastPasswordResetDate;

    @Transient
    private String currentToken;

    @Transient
    @ApiModelProperty(value="验证码",name="code")
    @JSONField(serialize = false)
    private String code;

//    @ApiModelProperty(value="权限",hidden = true)
//    @ManyToMany(cascade = {CascadeType.REFRESH}, fetch = FetchType.EAGER)
//    private List<SysRole> roles;




    @JSONField(serialize = false)
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<>();
        List<SysRole> roles = new ArrayList<>();
//        List<SysRole> roles = this.getRoles();
        for (SysRole role : roles) {
            auths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
            System.out.println("roleName:" + role.getName());
        }

        return auths;
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return this.password;
    }

    @JSONField(serialize = false)
    @Override
    public String getUsername() {
        return this.username;
    }

    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return true;
    }


    //维护sys_msg
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    private Set<SystemMsg> systemMsgs = new HashSet<>();
    //维护acitvity_msg
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    private Set<ActivityMsg> activityMsgs = new HashSet<>();


    @ManyToMany(fetch = FetchType.EAGER,cascade =  {CascadeType.REFRESH})
    private Set<Coupon> coupons;
}
